package com.yjd.bi.db.control;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yjd.bi.jf.service.IJfIntegralDataService;
import com.yjd.bi.jf.service.IJfIntegralService;
import com.yjd.bi.jf.service.IJfIntegralYearService;
import com.yjd.bi.jf.service.IJfOrderDetailProductSnapshotService;
import com.yjd.bi.jf.service.IJfOrderDetailService;
import com.yjd.bi.jf.service.IJfOrderService;
import com.yjd.bi.jf.service.IJfSignDataService;
import com.yjd.bi.user.service.IUserService;

import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.model.BiTableInfoModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.service.IBiTableInfoService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.enums.TableInfo;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.PagerUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.TableTransferUtil;

/**
 * <b>description</b>：表信息 <br>
 * <b>time</b>：2016-4-15下午4:13:48 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Controller
@RequestMapping(BiTableInfoControl.PRE_URL)
public class BiTableInfoControl extends BaseControl {
	public static final String PRE_URL = "/db/biTableInfo";
	public static final String PRE_ID = "db.biTableInfo";
	/**
	 * 根据表名迁移数据
	 * @param tablename
	 * @throws Exception
	 */
	@RequestMapping("/transfer")
	public void transferTable(String tablename,String schema) throws Exception{
		TableTransferUtil.TransTableCheck(schema,tablename);
		this.successMsg("操作成功！");
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list() throws Exception {
		Map paramMap = ServletUtil.getPageParameterMap(request);
		paramMap.put("sort", "id asc");
		paramMap.put("rows", 50);
		PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
				this.biTableInfoService.getPageModel(paramMap, DbWREnums.READ),
				ServletUtil.getPageParameterMap(request),
				ServletUtil.getUrl(PRE_URL + "/list"));
		this.request.setAttribute("pager", pager);
		List<BiDatabaseInfoModel> biDatabaseInfoModels = this.biDatabaseInfoService
				.getModelList(null, DbWREnums.READ);
		Map<Long, BiDatabaseInfoModel> biDatabaseInfoMap = FrameUtil
				.getBeanFieldToMap(biDatabaseInfoModels, "id");
		this.request.setAttribute("biDatabaseInfoModels", biDatabaseInfoModels);
		this.request.setAttribute("biDatabaseInfoMap", biDatabaseInfoMap);
		return getReturnUrl("list");
	}

	/**
	 * 跳转到新增页面
	 * 
	 * @param pid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/add")
	public String add() throws Exception {
		List<BiDatabaseInfoModel> biDatabaseInfoModels = this.biDatabaseInfoService
				.getModelList(null, DbWREnums.READ);
		this.request.setAttribute("biDatabaseInfoModels", biDatabaseInfoModels);
		return this.getReturnUrl("add");
	}

	/**
	 * 跳转到更新页面
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") long id) throws Exception {
		BiTableInfoModel biTableInfoModel = this.getService().getModelById(id,
				DbWREnums.READ);
		if (biTableInfoModel == null) {
			FrameUtil.throwBaseException1("该数据不存在!");
		}
		List<BiDatabaseInfoModel> biDatabaseInfoModels = this.biDatabaseInfoService
				.getModelList(null, DbWREnums.READ);
		this.request.setAttribute("biDatabaseInfoModels", biDatabaseInfoModels);
		this.request.setAttribute("biTableInfoModel", biTableInfoModel);
		return this.getReturnUrl("add");
	}

	/**
	 * 保存
	 * 
	 * @param model
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public void save(BiTableInfoModel model) throws Exception {
		if (model == null) {
			this.errorMsg("参数有误!");
		} else {
			if (model.getId() == null) {
				this.getService().insert(model);
				this.successMsg("操作成功!");
			} else {
				this.getService().update(model);
				this.successMsg(ServletUtil.getUrl(PRE_URL + "/list"), "操作成功!");
			}
		}
	}

	/**
	 * 根据数据源获取表列表
	 * 
	 * @param bi_database_info_id
	 * @throws Exception
	 */
	@RequestMapping("/getListJsonByDbId/{bi_database_info_id}")
	public void getListJsonByDbId(
			@PathVariable("bi_database_info_id") long bi_database_info_id)
			throws Exception {
		this.successData(FrameUtil.newHashMap("tableList",
				this.biTableInfoService.getModelList(FrameUtil.newHashMap(
						"database_info_id", bi_database_info_id),
						DbWREnums.READ)));

	}

	/**
	 * 更加对象id删除
	 * 
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable("id") long id) throws Exception {
		this.getService().delete(id);
		this.successMsg("删除成功!");
	}

	public String getReturnUrl(String stuff) {
		return PRE_ID + "." + stuff;
	}

	/**
	 * 数据库信息
	 */
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBiDatabaseInfoService biDatabaseInfoService;
	/**
	 * 表信息
	 */
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBiTableInfoService biTableInfoService;

	protected IBiTableInfoService getService() {
		return this.biTableInfoService;
	}
}
